var cover = null;
var messageDlg = null;
var contextPath;
var WSContextPath;

$(document).ready(function(){
	addFadeEffect();
});

function saveWSContextPath(ctxPath, WSPath) {
	contextPath = ctxPath;
	WSContextPath = WSPath;
}

function setCookie(key, value) {
	var str = escape(key) + "=" + escape(value);
	var date = new Date();
	var expires = 30*24*3600*1000;
	date.setTime(date.getTime() + expires);
	
	str += "; expires=" + date.toGMTString();			
	document.cookie = str;
}

function deleteCookie(key) {
	var date = new Date();
	date.setTime(date.getTime() - 1000);
	document.cookie = escape(key) + "=; expires=" + date.toGMTString();
}

function getCookie(key){//获取指定名称的cookie的值
	var arrStr = document.cookie.split("; ");
	for(var i = 0;i < arrStr.length;i ++){
		var temp = arrStr[i].split("=");
		if(unescape(temp[0]) == key) {
			return unescape(temp[1]);
		}
	} 
}

function addFadeEffect() {
	$(".fade_effect").unbind("mouseover mouseout");
	$(".fade_effect").mouseover(function(){
		$(this).fadeTo("speed", 0.5);
   	});
	$(".fade_effect").mouseout(function(){
		$(this).fadeTo(1, 1);
   	});
}

function addCover() {
	cover = document.createElement("div");
	$(cover).css("width", $(document).width());
	$(cover).css("height", $(document).height());
	$(cover).css("background", "url('" + contextPath + "/image/bg_cover.png')");
	$(cover).css("position", "absolute");
	$(cover).css("zIndex", "2000");
	$(cover).css("left", "0px");
	$(cover).css("top", "0px");
	$(document.body).append(cover);
}

function getTimeStr(miliSecond) {
	var seconds = Math.floor(miliSecond/1000);
	var secondPart = seconds%60;
	var minitues = Math.floor(seconds/60);
	var minituePart = minitues%60;
	var hours = Math.floor(minitues/60);
	var hourPart = hours%24;
	var dayPart = Math.floor(hours/24);
	
	var result = "";

	if (dayPart > 0) {
		result += dayPart + "天";
	}
	
	if (hourPart > 0) {
		result += hourPart + "小时";
	}
	
	if (minituePart > 0) {
		result += minituePart + "分";
	}
	
	return  result += secondPart + "秒";
}

function showCustomerLogin() {
	var requestUrl = contextPath + "/customer/login.jsp";
	$.post(requestUrl, "", function(data){
		addCover();
		$(document.body).append(data);
	});
}

function showCustomerRegister() {
	var requestUrl = contextPath + "/customer/register.jsp";
	$.post(requestUrl, "", function(data){
		try {
			cancelCustomerLogin();
		} catch (e) {
		}
		
		addCover();
		$(document.body).append(data);
	});
}

function showCustomerRegisterSuccess() {
	var requestUrl = contextPath + "/customer/register_success.jsp";
	$.post(requestUrl, "", function(data){
		try {
			cancelCustomerRegister();
		} catch (e) {
		}
		
		addCover();
		$(document.body).append(data);
	});
}

function showMerchantLogin() {
	var requestUrl = contextPath + "/merchant/login.jsp";
	$.post(requestUrl, "", function(data){
		addCover(contextPath);
		$(document.body).append(data);
	});
}

function getOrderStatusName(status) {
	switch (status) {
		case "0":
			return "等待养护";
			break;
		case "1":
			return "等待确认";
			break;
		case "2":
			return "等待评价";
			break;
		case "3":
			return "已完成";
			break;
	
		default:
			return "";
			break;
	}
}

function showMessage(msg) {
	messageDlg = document.createElement("div");
	$(messageDlg).css("width", "200px");
	$(messageDlg).css("padding", "15px");
	$(messageDlg).css("border", "1px #e3e3e3 solid");
	$(messageDlg).css("background", "url('" + contextPath + "/image/bg_cover.png')");
	$(messageDlg).css("position", "absolute");
	$(messageDlg).css("zIndex", "2000");
	$(messageDlg).css("left", ($(window).width() / 2 + $(document.body).scrollLeft()) - 100 + "px");
	$(messageDlg).css("top", ($(window).height() / 2 + $(document.body).scrollTop()) - 15 + "px");
	$(messageDlg).html(msg);
	$(document.body).append(messageDlg);
	
	$(messageDlg).fadeTo(3000, 0, function() {
		$(messageDlg).remove();
	});
}

function showTestMessage(e) {
	alert(e.toString());
}